/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pricecomparison;

import java.io.File;
import java.util.ArrayList;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 *
 * @author Jim Simon
 */
public class ExcelWriter {
    WritableWorkbook workbook;
    WritableSheet sheet;
    final int C_NAME = 0;
    final int C_PL_LIST = 1, C_PQ_MODEL = 1, C_DM_PRICE = 1, C_AL_PRICE = 1;
    final int C_PL_SALE = 2, C_PQ_COMPATIBLE = 2;
    final int C_PQ_OEM = 3;
    
    public final int MFER_PURELAND = 0;
    public final int MFER_PROJECTORQUEST = 1;
    public final int MFER_DISCOUNTMERCHANT = 2;
    public final int MFER_APEXLAMPS = 3;
    
    int[] cols = new int[4];
    
    public ExcelWriter(String filename) throws java.io.IOException {
        // Create Workbook
        workbook = Workbook.createWorkbook(new File("reports/" + filename + ".xls"));
    }
    
    public void newSheet(int mfer) throws jxl.write.WriteException {
        Label label;
        switch (mfer) {
            case MFER_PURELAND:
                sheet = workbook.createSheet("Pureland", 0);
                label = new Label(C_NAME, 0, "Item Name");
                sheet.addCell(label);
                label = new Label(C_PL_LIST, 0, "Pureland List Price");
                sheet.addCell(label);
                label = new Label(C_PL_SALE, 0, "Pureland Sale Price");
                sheet.addCell(label);
                cols[0] = C_NAME;
                cols[1] = C_PL_LIST;
                cols[2] = C_PL_SALE;
                break;
            case MFER_PROJECTORQUEST:
                sheet = workbook.createSheet("Projector Quest", 1);
                label = new Label(C_NAME, 0, "Item Name");
                sheet.addCell(label);
                label = new Label(C_PQ_MODEL, 0, "Model Number");
                sheet.addCell(label);
                label = new Label(C_PQ_COMPATIBLE, 0, "Projector Quest Comaptible Price");
                sheet.addCell(label);
                label = new Label(C_PQ_OEM, 0, "Projector Quest OEM Price");
                sheet.addCell(label);
                cols[0] = C_NAME;
                cols[1] = C_PQ_MODEL;
                cols[2] = C_PQ_COMPATIBLE;
                cols[3] = C_PQ_OEM;
                break;
            case MFER_DISCOUNTMERCHANT:
                sheet = workbook.createSheet("Discount-Merchant", 2);
                label = new Label(C_NAME, 0, "Item Name");
                sheet.addCell(label);
                label = new Label(C_DM_PRICE, 0, "Price");
                sheet.addCell(label);
                cols[0] = C_NAME;
                cols[1] = C_DM_PRICE;
                break;
            case MFER_APEXLAMPS:
                sheet = workbook.createSheet("Apex Lamps", 3);
                label = new Label(C_NAME, 0, "Item Name");
                sheet.addCell(label);
                label = new Label(C_AL_PRICE, 0, "Price");
                sheet.addCell(label);
                cols[0] = C_NAME;
                cols[1] = C_AL_PRICE;
                break;
        }
        return;
    }
    
    public void writeItem(ArrayList items, int mfer) throws jxl.write.WriteException {
        // Get lenghth of sheet
        int pos = sheet.getColumn(C_NAME).length;
        
        // Create labels
        for (int i = 0; i < items.size(); i++) {
            Label label = new Label(cols[i], pos, items.get(i).toString());
            sheet.addCell(label);
        }
    }
}
